package com.qymerge.manage.base.service.impl;

import com.qymerge.manage.base.dao.IUserWXDao;
import com.qymerge.manage.base.dto.UserWXDto;
import com.qymerge.manage.base.dto.export.UserWXExportDTO;
import com.qymerge.manage.base.ro.UserWXRO;
import com.qymerge.manage.base.service.IUserWXService;
import com.qymerge.manage.core.bean.ActionResultRO;
import com.qymerge.manage.core.bean.DataTableRO;
import com.qymerge.manage.core.bean.PageRequest;
import com.qymerge.manage.core.utils.ReadExcelUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

/**
 *<p>Title: UserWXServiceImpl</p>
 *<p>Description: 用户信息</p>
 *
 *@Author cong.zhang
 *@Version 1.0.0
 *@Date 2020/12/18
 *@Time 23:38
 */
@Service
public class UserWXServiceImpl implements IUserWXService {

    @Autowired
    private IUserWXDao userWXDao;

    /**
     * 列表
     *
     * @param ro
     * @param pageRequest
     * @return
     */
    @Override
    public DataTableRO list(UserWXDto ro, PageRequest pageRequest) {
        List list = userWXDao.list(ro, pageRequest);
        DataTableRO dataTableRO = new DataTableRO();
        int allCount = userWXDao.total(ro);
        int pageSize = pageRequest.getSize();
        dataTableRO.setAll_count(allCount);
        dataTableRO.setList(list);
        dataTableRO.setCurrent_page(pageRequest.getPage());
        dataTableRO.setPage_size(pageSize);
        int allPage = (allCount % pageSize == 0 ? allCount / pageSize : allCount / pageSize + 1);
        dataTableRO.setAll_page(allPage);
        return dataTableRO;
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Override
    public UserWXRO detail(Long id) {
        return userWXDao.findOne(id);
    }

    /**
     * 新增
     *
     * @param ro
     * @return
     */
    @Override
    public ActionResultRO save(UserWXRO ro) {
        int rn = 0;
        if (ro.getId() == 0) {
            rn = userWXDao.install(ro);
        } else {
            rn = userWXDao.update(ro);
        }
        return new ActionResultRO(rn, ro.getId() == 0 ? "保存成功" : "修改成功", ro.getId() == 0 ? "保存失败" : "修改失败");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Override
    public ActionResultRO delete(Long[] ids) {
        int rn = userWXDao.remove(Arrays.asList(ids));
        return new ActionResultRO(true, "删除" + rn +"成功", "删除" + rn +"失败");
    }

    /**
     *
     * @param multipartFile
     * @return
     */
    @Override
    public ActionResultRO importData(MultipartFile multipartFile, int flag) {
        StringBuffer tip = new StringBuffer("导入结果：<br/>");
        ReadExcelUtil readExcelUtil = new ReadExcelUtil();
        List<String[][]> datas = null;
        try {
            datas = readExcelUtil.readByStream(multipartFile.getInputStream(), 2, multipartFile.getOriginalFilename());
            if (org.springframework.util.CollectionUtils.isEmpty(datas)) {
                return new ActionResultRO(false, "execl数据为空");
            }
            String[][] data = datas.get(0);

            long id= 0L;
            String openid= null;
            String token= null;
            String username= null;
            String mobile= null;
            String addr= null;
            String email= null;
            String creaetdate= null;
            String lastlogin= null;
            String ip= null;

            UserWXRO userWXRO = null;
            int rowNum = 1;        // 记录当前行
            int successNum = 0;    // 记录新增或者更新记录数
            for (String[] row : data) {
                rowNum++;

                id = Long.valueOf(row[0]);
                openid= row[1];
                token= row[2];
                username= row[3];
                mobile= row[4];
                addr= row[5];
                email= row[6];
                creaetdate= row[7];
                lastlogin= row[8];
                ip= row[9];

                if (StringUtils.isEmpty(id+"") || StringUtils.isEmpty(mobile) || StringUtils.isEmpty(creaetdate) || StringUtils.isEmpty(lastlogin)) {
                    tip.append("第").append(rowNum).append("行：部分数据为空，不予新增<br/>");
                    continue;
                }

                userWXRO = new UserWXRO(id, openid, token, username, mobile, addr, email, creaetdate, lastlogin, ip);

                if (flag == 1) {
                    userWXDao.install(userWXRO);
                    successNum++;
                } else if (flag == 0) {
                    userWXDao.update(userWXRO);
                    successNum++;
                }

            }
            tip.append("成功"+ (flag == 1 ? "新增":"更新") +"记录：").append(successNum).append("条");
        } catch (IOException e) {
            e.printStackTrace();
            return new ActionResultRO(false, "导入失败");
        }

        return new ActionResultRO(true, tip.toString());
    }

    /**
     * 导出表格
     *
     * @param ro
     * @return
     */
    @Override
    public List<UserWXExportDTO> exportData(UserWXDto ro) {
        List<UserWXExportDTO> list = new ArrayList<UserWXExportDTO>();
        UserWXExportDTO dto = null;

        List<UserWXRO> userWXROS = userWXDao.findAll(ro);
        if (CollectionUtils.isNotEmpty(userWXROS)) {

            int num = 1;
            for (UserWXRO userWXRO : userWXROS) {
                dto = new UserWXExportDTO();
//                dto.setNum(num);
//                num++;

                dto.setId(userWXRO.getId());
                dto.setOpenid(userWXRO.getOpenid());
                dto.setToken(userWXRO.getToken());
                dto.setUsername(userWXRO.getUsername());
                dto.setMobile(userWXRO.getMobile());
                dto.setAddr(userWXRO.getAddr());
                dto.setEmail(userWXRO.getEmail());

                dto.setCreaetdate(userWXRO.getCreaetdate());
                dto.setLastlogin(userWXRO.getLastlogin());

                dto.setIp(userWXRO.getIp());

                list.add(dto);
            }

        }
        return list;
    }


}
